package org.example.controller;

import org.example.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class UserController {
    // 普通参数
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name) {
        System.out.println("普通参数传递 name ==》" + name);
        return "{'module':'common param'}";
    }

    // 普通参数：请求参数名与形参名不同
    @RequestMapping("/commonParamDifferentName")
    @ResponseBody
    public String commonParamDifferentName(@RequestParam("name") String userName, int age) {
        System.out.println("普通参数传递 name ==》" + userName);
        System.out.println("普通参数传递 age ==》" + age);
        return "{'module':'common param different name'}";
    }

    // pojo参数
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user) {
        System.out.println("pojo参数传递 user ==》" + user);
        return "{'module':'pojo param'}";
    }

    // 嵌套pojo参数
    @RequestMapping("/pojoContainPojoParam")
    @ResponseBody
    public String pojoContainPojoParam(User user) {
        System.out.println("pojo嵌套pojo参数传递 user ==》" + user);
        return "{'module':'pojo contain pojo param'}";
    }

    // 数组参数
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes) {
        System.out.println("数组参数传递 likes ==》" + Arrays.toString(likes));
        return "{'module':'array param'}";
    }

    //集合参数
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> list) {
        System.out.println("集合参数传递 list ==》" + list);
        return "{'module':'list param'}";
    }


    // json格式
    // 集合参数
    @RequestMapping("/listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> list) {
        System.out.println("集合(json)参数传递 list ==》" + list);
        return "{'module':'list param for json'}";
    }

    // pojo参数
    @RequestMapping("/pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user) {
        System.out.println("pojo(json)参数传递 user ==》" + user);
        return "{'module':'pojo param for json'}";
    }

    // pojo集合参数
    @RequestMapping("/pojoListParamForJson")
    @ResponseBody
    public String pojoListParamForJson(@RequestBody List<User> users) {
        System.out.println("pojo集合(json)参数传递 list ==》" + users);
        return "{'module':'pojo list param for json'}";
    }


    // 日期参数
    @RequestMapping("/dateParam")
    @ResponseBody
    public String dataParam(Date date, @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1) {
        System.out.println("日期参数传递 date ==》" + date);
        System.out.println("日期参数传递 date1 ==》" + date1);
        return "{'module':'date param'}";
    }
}
